<?php

/**
 * Ownchoice form.
 *
 * @package    briefing
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class OwnchoiceForm extends BaseOwnchoiceForm
{
  public function bindEmbeddedForms($embedded_forms, $values) 
  {
    if ($this->isValid()) 
    {
      foreach ($embedded_forms as $name => $form) 
      {
        $form->isBound = true;
        $form->values = $values[$name];

        if ($form->embeddedForms) 
        {
          $this->bindEmbeddedForms($form->embeddedForms, $values[$name]);
        }
      }
    }
  }
  
  public function bind(array $taintedValues = null, array $taintedFiles = null) 
  {

      parent::bind($taintedValues, $taintedFiles);
      $this->bindEmbeddedForms($this->embeddedForms, $this->getValues());

  }
  
  
  public function saveEmbeddedForms($con = null, $forms = null)
  {
    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    if (is_null($forms))
    {
      $forms = $this->embeddedForms;
    }

    foreach ($forms as $key => $form)
    {
      if ($form instanceof sfFormDoctrine)
      {
      	/*
      	 * --------------------
      	 * only modification
      	 */
      	if(method_exists(new $form(), 'doSaveManyToMany'))
      	{
          $form->doSaveManyToMany($con);
      	}
      	else
      	{
          $form->getObject()->save($con);
      	}
      	/*
      	 * --------------------
      	 */
        $form->saveEmbeddedForms($con);
      }
      else
      {
        $this->saveEmbeddedForms($con, $form->getEmbeddedForms());
      }
    }
  }
  
  /**TODO
   * it is not saving at the moment
   */
  public function doSaveManyToMany($con = null)
  {
    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $this->object->save($con);

   /*
    * Save the many-2-many relationship
    */
    /*
     $this->save***List($con) is a generic notation since when your
     M-N relationships in symfony, it automatically generates 
     saveRelationsList() methods in the baseForm class of your object
     So it could be saveGroupsList() for instance
     */
    //$this->save***List($con);
    
  }
  
  
  
  public function configure()
  {
    
    //refactoring
    $form = new EditionoptionsCollectionForm(null, array(
        'ownchoiceid' => $this->getObject(),
    ));
    $this->embedForm('Own choice form', $form);
    
    
    //widget and validators
//    $this->validatorSchema['ownchoiceid'] = new sfValidatorPass();
    
    
    
    
    
    
    
    //old style
//    $subForm = new sfForm();
//    $subSubForm = new sfForm();
//    
//    $clientOrder = new Clientorder();
//    $editionOptions = new Editionoptions();
//    
//    $editionOptions->setOwnchoice($this->getObject());
//    $clientOrder->setEditionoptions($editionOptions);
//    
//    $form = new ClientorderForm($clientOrder);
//    $subForm->embedForm('Client order form', $form);
//    $subSubForm->embedForm('edition options form', $subForm);
//    
//    $this->embedForm('Own choice form', $subSubForm);
    
    
    
  }
  
//  public function saveEmbeddedForms($con = null, $forms = null)
//  {
//    if (null === $forms)
//    {
//      $photos = $this->getValue('newPhotos');
//      $forms = $this->embeddedForms;
//      foreach ($this->embeddedForms['newPhotos'] as $name => $form)
//      {
//        if (!isset($photos[$name]))
//        {
//          unset($forms['newPhotos'][$name]);
//        }
//      }
//    }
//
//    return parent::saveEmbeddedForms($con, $forms);
//  }
}
